package com.zwface.demo.function.main;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import android.os.Bundle;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.tab.QMUITab;
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;
import com.zwface.demo.R;
import com.zwface.demo.base.BaseFragment;
import com.zwface.demo.base.CheckPermissionsActivity;
import com.zwface.demo.databinding.ActivityMainBinding;
import com.zwface.demo.function.attendance.AttendanceFragment;
import com.zwface.demo.function.mine.MineFragment;

public class MainActivity extends CheckPermissionsActivity<ActivityMainBinding>
{
    private BaseFragment mainFragment;
    private BaseFragment attendanceFragment;
    private BaseFragment mineFragment;
    private String TAG = "MainActivity";
    @Override
    public int onLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onInitData(Bundle savedInstanceState) {
        super.onInitData(savedInstanceState);
    }

    //获取权限后回调
    @Override
    public void onCheckPremission() {
    }

    @Override
    public void onInitComponent() {
        super.onInitComponent();
        mainFragment = new MainFragment();
        attendanceFragment = new AttendanceFragment();
        mineFragment = new MineFragment();
        initTabs();
        initPagers();
    }

    private FragmentStatePagerAdapter fragmentStatePagerAdapter =
            new FragmentStatePagerAdapter(getSupportFragmentManager()) {
                @NonNull
                @Override
                public Fragment getItem(int position) {
                    BaseFragment baseFragment = null;
                    switch (position){
                        case 0: baseFragment = mainFragment; break;
                        case 1: baseFragment = attendanceFragment; break;
                        case 2: baseFragment = mineFragment; break;
                    }
                    return baseFragment;
                }

                @Override
                public int getCount() {
                    return 3;
                }
            };

    private void initTabs()
    {
        QMUITabBuilder builder = binding.tabs.tabBuilder();
        builder.setSelectedIconScale(1f)
                .setTextSize(QMUIDisplayHelper.sp2px(context, 13), QMUIDisplayHelper.sp2px(context, 13))
                .setDynamicChangeIconColor(false);
        QMUITab main = builder
                .setNormalDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_tabbar_main))
                .setSelectedDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_tabbar_main))
                .setText("课表")
                .build(context);

        QMUITab attendance = builder
                .setNormalDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_tabbar_attendance))
                .setSelectedDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_tabbar_attendance))
                .setText("功能")
                .build(context);

        QMUITab mine = builder
                .setNormalDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_tabbar_mine))
                .setSelectedDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_tabbar_mine))
                .setText("我的")
                .build(context);

        binding.tabs.addTab(main)
                .addTab(attendance)
                .addTab(mine);
    }

    private void initPagers() {
        binding.pager.setOffscreenPageLimit(3);//设置预加载最大数量
        binding.pager.setAdapter(fragmentStatePagerAdapter);
        binding.tabs.setupWithViewPager(binding.pager, false);
        binding.tabs.setMode(QMUITabSegment.MODE_FIXED);
    }


}
